﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "EditInsBook" here, you must also update the reference to "EditInsBook" in App.config.
    public partial class MainService : IEditInsBook
    {
        //public int EditInsBook_Using(int EditInsBookID)
        //{
        //    int retVal = 0;
        //    SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
        //    SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_Using", dbConn);
        //    dbCmd.CommandType = CommandType.StoredProcedure;
        //    dbCmd.Parameters.AddWithValue("@EditInsBookID", EditInsBookID);
        //    try
        //    {
        //        dbConn.Open();
        //        retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
        //    }
        //    finally
        //    {
        //        dbConn.Close();
        //    }
        //    return retVal;
        //}

        public DataSet EditInsBook_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        

        public DataSet EditInsBook_GetList(out int ItemCount, int _month, int _year, int branchID, int departmentID,int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", departmentID);
            dbCmd.Parameters.AddWithValue("@BranchID", branchID);            
            dbCmd.Parameters.AddWithValue("@month", _month);
            dbCmd.Parameters.AddWithValue("@year", _year);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public EditInsBookInfo EditInsBook_GetInfo(int EditInsBookID)
        {
            EditInsBookInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EditInsBook_ID", EditInsBookID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new EditInsBookInfo();
                    retVal.ID = Convert.ToInt32(dr["EditInsBook_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["EditInsBook_EmployeeID"]);
                    retVal.Content = dr["EditInsBook_Content"].ToString();
                    retVal.Old = dr["EditInsBook_Old"].ToString();
                    retVal.New = dr["EditInsBook_New"].ToString();
                    retVal.Cause = dr["EditInsBook_Cause"].ToString();
                    retVal.EditDate = dr["EditInsBook_Date"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EditInsBook_Date"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int EditInsBook_Insert(EditInsBookInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EditInsBook_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@EditInsBook_Content", eInfo.Content);
            dbCmd.Parameters.AddWithValue("@EditInsBook_Old", eInfo.Old);
            dbCmd.Parameters.AddWithValue("@EditInsBook_New", eInfo.New);
            dbCmd.Parameters.AddWithValue("@EditInsBook_Cause", eInfo.Cause);
            if (eInfo.EditDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EditInsBook_Date", eInfo.EditDate);
            else
                dbCmd.Parameters.AddWithValue("@EditInsBook_Date", DBNull.Value);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool EditInsBook_Update(EditInsBookInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EditInsBook_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@EditInsBook_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@EditInsBook_Content", eInfo.Content);
            dbCmd.Parameters.AddWithValue("@EditInsBook_Old", eInfo.Old);
            dbCmd.Parameters.AddWithValue("@EditInsBook_New", eInfo.New);
            dbCmd.Parameters.AddWithValue("@EditInsBook_Cause", eInfo.Cause);
            if (eInfo.EditDate != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EditInsBook_Date", eInfo.EditDate);
            else
                dbCmd.Parameters.AddWithValue("@EditInsBook_Date", DBNull.Value);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool EditInsBook_Delete(int EditInsBookID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EditInsBook_ID", EditInsBookID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet EditInsBook_Report(int _branchID, int _month, int _year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EditInsBook_Report", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@BranchID", _branchID);
            dbCmd.Parameters.AddWithValue("@month", _month);
            dbCmd.Parameters.AddWithValue("@year", _year);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

